package com.shop.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.model.entity.ConsultTb;
import com.shop.model.entity.UserTb;
import com.shop.model.mapper.ConsultTbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shop
 * @since 2018-12-04
 */
@Controller
public class ConsultTbController {
    @Autowired
    private ConsultTbMapper consultTbMapper;
    @RequestMapping("/information")
    public String Information(HttpServletRequest request) {
        //分页查询
        int nowpage1=1;
        //对当前页进行判断是否为空
        String nowpage = request.getParameter("nowpage");
        if (null!=nowpage) {
            //将string转为int类型
            nowpage1 = Integer.parseInt(nowpage);
        }
        //查询有多少条数据
        Integer integer = consultTbMapper.selectCount(null);
        //将数据总条数传到前端
        request.setAttribute("count",integer);
        //用IPage对对象Consult进行分页查询
        IPage<ConsultTb> iPage = consultTbMapper.selectPage(new Page<>(nowpage1, 4), null);
        //将当前页传到前端
        request.setAttribute("nowpage",nowpage1);
        List<ConsultTb> records = iPage.getRecords();

        //遍历对象consult
//        for (ConsultTb list : records) {
//            System.out.println(list);
//        }

        //将对象数据传到前端用Thymeleaf的th标签来接受
        request.setAttribute("list", records);
        return "information";
    }


    //模糊问题查询
    @RequestMapping("/like")
    public ConsultTb body(String question) {
        QueryWrapper<ConsultTb> wrapper = new QueryWrapper<>();
        wrapper.eq("consult_questtion", "%#{" + question + "}%");
        ConsultTb consultTb = consultTbMapper.selectOne(wrapper);
        System.out.println("+=++==++++++======="+consultTb);
        return consultTb;
    }
}
